<template>
  <div class="Login">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="account">
        <el-input v-model="ruleForm.account"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <div v-if="codenumber > 0">
        <el-form-item label="验证码" prop="checkcode">
          <el-input v-model="ruleForm.checkcode"></el-input>
        </el-form-item>
        <el-form-item>
          <CodeData v-on:childByValue="childByValue"></CodeData>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <div></div>
  </div>
</template>
<script>
import CodeData from "@/components/CodeData.vue";

export default {
  components: {
    CodeData,
  },

  data() {
    return {
      // 次数
      codenumber: 0,
      ruleForm: {
        account: "",
        password: "",
        // 验证码
        checkcode: "",
        // 系统验证码-老的，是独立得
        sysCode: [],
        // 系统验证码，新的
        sysCodeNew: [],
      },
      rules: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // 提交次数+1
      this.codenumber++;
      console.log(this.codenumber);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 监听子组件传递过来得验证码
    childByValue: function (childValue) {
      // childValue就是子组件传过来的值
      this.sysCode = childValue;
      this.sysCodeNew = [];
      this.sysCode.forEach((element) => {
        this.sysCodeNew.push(element);
      });
      console.log(this.sysCodeNew, "系统生成的四位数验证码");
    },
  },
};
</script>

<style lang="less" scoped>
.Login {
  width: 400px;
  height: auto;
  border: 1px solid skyblue;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 50px;
  transform: translate(-50%, -50%);
}
::v-deep .el-form-item__label {
}
</style>
